<!-- 流程模型管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" class="search-form" size="small" :inline="true" v-show="showSearch" label-width="">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <!-- 右边浮动按钮 -->
      <div class="right-btn">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activiti:modeler:create']"
        >新增</el-button>
      </div>
    </el-form>
    <!-- @selection-change="handleSelectionChange" -->
    <!-- :max-height="height" :height="height" -->
    <el-table v-loading="loading" :data="tableList" :max-height="height">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" fixed="left">
        <template slot-scope="scope">
          <span>{{scope.$index+1 + (queryParams.pageNum-1)*queryParams.pageSize}}</span>
        </template>
      </el-table-column>
      <el-table-column label="KEY" align="center" prop="key" />
      <el-table-column label="名称" align="center" prop="name" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="状态" align="center" prop="deploymentId">
        <template slot-scope="scope">
          <span v-if="scope.row.deploymentId">已部署</span>
          <span v-else>未部署</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ datetime_format(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="lastUpdateTime" width="150">
        <template slot-scope="scope">
          <span>{{ datetime_format(scope.row.lastUpdateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="元数据" align="center" prop="metaInfo" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click.native="handleEdit(scope.row)"
            v-hasPermi="['activiti:modeler:editor']"
          >编辑</el-button>
          <!-- v-hasPermi="['activiti:modeler:editor']" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click.native="handleDeploy(scope.row)"
            v-hasPermi="['activiti:modeler:deploy']"
          >部署</el-button>
          <!-- v-hasPermi="['activiti:modeler:deploy']" -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
          >导出</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activiti:modeler:remove']"
          >删除</el-button>
          <!-- v-hasPermi="['activiti:modeler:remove']" -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加流程模型弹出框 -->
    <el-dialog title="添加流程模型" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称：" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="KEY：" prop="key">
              <el-input v-model="form.key" placeholder="请输入KEY值" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述：">
              <el-input v-model="form.desc" placeholder="请输入描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="submitLoading">确定</el-button>
        <el-button @click="open = false">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listProcessModel,addProcessModel,delProcessModel,bsProcessModel,exportProcessModel } from "@/api/system/processModelManage";

export default {
  name: "ProcessModelManage",
  components: {},
  data() {
    return {
      height: (document.documentElement.clientHeight || document.body.clientHeight)-220,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻资讯表格数据
      tableList: [],
      // 是否显示弹出层
      open: false,
      // 表单提交加载项
      submitLoading: false,
      form: {},
      rules: {
        name: [{ required:true, message:'请输入名称', trigger:'blur' }],
        key: [{ required:true, message:'请输入KEY值', trigger:'blur' }],
        desc: [{ required:true, message:'请输入描述', trigger:'blur' }]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询流程模型管理列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams)
      listProcessModel(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /* 重置表单 */
    reset(){
      this.form = {
        name: undefined,
        key: undefined,
        desc: undefined
      }
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
    },
    /** 表单提交操作 */
    submitForm(){
      this.$refs['form'].validate(valid => {
        if(valid){
          this.submitLoading = true
          addProcessModel(this.form).then((res)=>{
            this.submitLoading = false
            if(res.code == 200){
              this.open = false
              this.msgSuccess(res.msg)
              this.getList()
              if(res.data){
                this.$store.dispatch('tagsView/delView', {name: 'WorkActivity'}).then(({ visitedViews }) => {
                  this.$router.push({
                    name: 'WorkActivity',
                    params: {
                      id: res.data
                    }
                  })
                })
              }
            }
          }).then(()=>{
            this.submitLoading = false
          })
        }
      })
    },
    /* 部署按钮操作 */
    handleDeploy(row){
      var that = this
      that.$confirm('是否确认部署?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          // return bsProcessModel(row.id);
          bsProcessModel(row.id).then((res)=>{
            console.log(res)
            if(res.code == 200){
              that.msgSuccess(res.msg);
              that.getList();
            }
          })
        }).then(() => {
        }).catch(function() {});
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      if(row && row.id>0){
        this.$store.dispatch('tagsView/delView', {name: 'WorkActivity'}).then(({ visitedViews }) => {
          this.$router.push({
            name: 'WorkActivity',
            params: {
              id: row.id
            }
          })
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this
      const informationIds = row.id || that.ids;
      var params = { ids:informationIds }
      that.$confirm('是否确认删除流程模型名称为"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProcessModel(params);
        }).then(() => {
          that.getList();
          that.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport(row) {
      var that = this
      const queryParams = that.queryParams;
      that.$confirm('是否确认导出?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          exportProcessModel(row.id).then((res)=>{
            console.log(res)
            // var a = document.createElement('a');
            // var url = createObjectURL(blob);
            // a.href = url;
            // a.download = filename;
            // document.body.appendChild(a);
            // a.click();
            // window.URL.revokeObjectURL(url);
            // if(res.code == 200){
            //   //this.msgSuccess(res.msg)
            //   this.download(res.msg);
            // }
          })
          //return exportInformation(queryParams);
        }).then(response => {
        }).catch(function() {});
    },
    //下载
    downloadAttachment(row) {
      if (row.attachmentName != null){
        var attachmentName = row.attachmentName.split(",");
        var attachmentUrl = row.attachmentUrl.split(",");
        for (var k=0;k<attachmentName.length;k++){
          var name = attachmentName[k];
          var url = attachmentUrl[k];
          const a = document.createElement('a');
          a.setAttribute('download', name);
          a.setAttribute('title', name);
          a.setAttribute('target', '_blank');
          a.setAttribute('href', url);
          a.click();
        }
      }
    },
  }
};
</script>
